package com.aivin.waterfallflow.tool;
import com.aivin.waterfallflow.bean.HeartBeatTask;
import com.aivin.waterfallflow.callback.IGetOneShareSpiderCallBack;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class SpiderOneShareDataTool {
    private WkHeartBeatTool wkHeartBeatTool    ;
    private final int TIMEOUT =10 *1000 ;
    public SpiderOneShareDataTool(){
        wkHeartBeatTool = new WkHeartBeatTool() ;
    }

    public void getOneShareData(final String webUrl , final IGetOneShareSpiderCallBack iGetOneShareSpiderCallBack , final int type ){
        iGetOneShareSpiderCallBack.onStartLoad();
        wkHeartBeatTool.startDelayedAction(new HeartBeatTask() {
            @Override
            public void run() {
                try {
                    Document doc = Jsoup.connect(webUrl).timeout(TIMEOUT).get();
                    Elements divs = doc.select("body");
                    if(divs==null ||divs.size()<1){
                        return;
                    }
                    Element div=divs.get(0);
                    iGetOneShareSpiderCallBack.onGetData(div.text() ,type);

                } catch (Exception e) {
                    e.printStackTrace();
                    iGetOneShareSpiderCallBack.onError(e.getMessage());
                }
            }
        } ,0);

    }
}
